package com.example.sunmiv1.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.example.sunmiv1.R;
import com.example.sunmiv1.activity.StringCodeOperationActivity;
import com.example.sunmiv1.entity.WarehousingDetailInfo;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.ACache;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WarehousingDetailAdapter extends BaseAdapter {

    private Context context;
    private List<WarehousingDetailInfo.DataBean> list;
    private ViewHolder viewHolder;
    private String isWarehousing;
    private String pricePermission;// 門店查看價格權限
    private String type = "";

    public WarehousingDetailAdapter(Context context, List<WarehousingDetailInfo.DataBean> list, String isWarehousing) {
        this.context = context;
        this.list = list;
        this.isWarehousing = isWarehousing;
        ACache aCache = ACache.get(context);
        pricePermission = aCache.getAsString("showPrice");

    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_warehousing_detail_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        WarehousingDetailInfo.DataBean dataBean = list.get(position);
        if ("2".equals(pricePermission)) {
            viewHolder.layoutPPriceWarehousingDetail.setVisibility(View.GONE);
            viewHolder.layoutPTotalWarehousingDetail.setVisibility(View.GONE);
        }
        viewHolder.numWarehousingDetail.setText(context.getResources().getString(R.string.warehousingDetail_num) + dataBean.getIndex());
        viewHolder.nameWarehousingDetail.setText(dataBean.getGoods_name());
        Tools.hideViewInItem(dataBean.getCompany(), viewHolder.goodsCompany);
        Tools.hideViewInItem(dataBean.getSku_name(), viewHolder.skuName);

        viewHolder.skuName.setText(dataBean.getSku_name());
        viewHolder.goodsCode.setText(dataBean.getGoods_code());
        viewHolder.freightNumberWarehousingDetail.setText(dataBean.getGoods_number());
        viewHolder.skuNumWarehousingDetail.setText(dataBean.getSku_num() + "");
        if ("1".equals(isWarehousing)) {
            viewHolder.countWarehousingDetail.setText(dataBean.getStock_num() + "");
            viewHolder.priceWarehousingDetail.setText(dataBean.getStock_price() + "");
        } else {
            viewHolder.tvPriceType.setText(R.string.allocation_tube_details_warehouse_price);
            viewHolder.countWarehousingDetail.setText(dataBean.getWarehouse_num() + "");
            viewHolder.priceWarehousingDetail.setText(dataBean.getWarehouse_price() + "");
        }
        viewHolder.totalWarehousingDetail.setText(dataBean.getSubtotal() + "");

        Tools.hideViewInItem(dataBean.getRemarks(), viewHolder.tvRemark);

        if ("1".equals(list.get(position).getSerial_number_state())) {
            viewHolder.countWarehousingDetail.setTextColor(ContextCompat.getColor(context, R.color.blue_3293FA));
        } else {
            viewHolder.countWarehousingDetail.setTextColor(ContextCompat.getColor(context, R.color.text_666));
        }
        viewHolder.countWarehousingDetail.setOnClickListener(v -> {
            if ("1".equals(list.get(position).getSerial_number_state())) {
                Intent intent = new Intent(context, StringCodeOperationActivity.class);
                if ("1".equals(isWarehousing)) {
                    if ("4".equals(type) || "6".equals(type)) {
                        intent.putExtra("operation", 22);
                    } else {
                        intent.putExtra("operation", 23);
                    }
                } else {
                    intent.putExtra("operation", 24);
                }
                intent.putExtra("id", list.get(position).getId());
                context.startActivity(intent);
            }
        });

        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.num_warehousingDetail)
        TextView numWarehousingDetail;
        @BindView(R.id.name_warehousingDetail)
        TextView nameWarehousingDetail;
        @BindView(R.id.sku_name)
        TextView skuName;
        @BindView(R.id.layout_sku_name)
        RelativeLayout layoutSkuName;
        @BindView(R.id.freightNumber_warehousingDetail)
        TextView freightNumberWarehousingDetail;
        @BindView(R.id.goods_code)
        TextView goodsCode;
        @BindView(R.id.layout_goods_code)
        RelativeLayout layoutGoodsCode;
        @BindView(R.id.goods_company)
        TextView goodsCompany;
        @BindView(R.id.layout_company)
        RelativeLayout layoutCompany;
        @BindView(R.id.skuNum_warehousingDetail)
        TextView skuNumWarehousingDetail;
        @BindView(R.id.count_warehousingDetail)
        TextView countWarehousingDetail;
        @BindView(R.id.price_warehousingDetail)
        TextView priceWarehousingDetail;
        @BindView(R.id.layout_p_price_warehousingDetail)
        LinearLayout layoutPPriceWarehousingDetail;
        @BindView(R.id.total_warehousingDetail)
        TextView totalWarehousingDetail;
        @BindView(R.id.layout_p_total_warehousingDetail)
        LinearLayout layoutPTotalWarehousingDetail;
        @BindView(R.id.tv_price_type)
        TextView tvPriceType;
        @BindView(R.id.tv_remark)
        TextView tvRemark;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
